package org.jeecg.modules.fund.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.fund.entity.FundList;
import org.jeecg.modules.fund.entity.FundMoving;
import org.jeecg.modules.fund.entity.FundVal;
import org.jeecg.modules.fund.mapper.FundListMapper;
import org.jeecg.modules.fund.service.IFundListService;
import org.jeecg.modules.fund.service.IFundMovingService;
import org.jeecg.modules.fund.service.IFundValService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "基金管理")
@RequestMapping("/sys/fund")
@Slf4j
public class FundListController {
    @Autowired
    private FundListMapper fundListMapper;
    @Autowired
    private IFundListService fundListService;
    @Autowired
    private IFundValService fundValService;
    @Autowired
    private IFundMovingService fundMovingService;

    /**
     * 分页列表查询
     * @param fundList
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<FundList>> queryPageList(FundList fundList,
                                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                 HttpServletRequest req) {
        QueryWrapper<FundList> queryWrapper = QueryGenerator.initQueryWrapper(fundList, req.getParameterMap());
        Page<FundList> page = new Page<>(pageNo, pageSize);
        IPage<FundList> pageList = fundListService.page(page, queryWrapper);
        pageList.getRecords().forEach(item -> {
            // 获取最新的基金净值数据
            FundVal lastVal = fundValService.getOne(new LambdaQueryWrapper<FundVal>().eq(FundVal::getFundId,item.getId()).orderByDesc(FundVal::getValDate).last("LIMIT 1"));
            // 获取最新的基准线数据
            FundMoving moving = fundMovingService.getOne(new LambdaQueryWrapper<FundMoving>().eq(FundMoving::getFundId,item.getId()).eq(FundMoving::getType,60).orderByDesc(FundMoving::getValDate).last("LIMIT 1"));
            // 数据均不为空，才能获取提示数据
            if(lastVal != null && moving != null){
                // 安全值
                BigDecimal safeVal = item.getAqqVal().multiply(moving.getValMoving());
                // 加仓值
                BigDecimal addVal =item.getAcVal().multiply(moving.getValMoving());
                // 危险区
                BigDecimal dangerVal = item.getWxqVal().multiply(moving.getValMoving());
                // 减仓值
                BigDecimal jcVal = item.getJcVal().multiply(moving.getValMoving());
                if (safeVal.compareTo(lastVal.getNowVal()) > 0) {
                    item.setTip("安全区");
                } else if (addVal.compareTo(lastVal.getNowVal()) > 0) {
                    item.setTip("加仓");
                } else if (lastVal.getNowVal().compareTo(dangerVal) > 0) {
                    item.setTip("危险区");
                } else if(lastVal.getNowVal().compareTo(jcVal) > 0) {
                    item.setTip("减仓");
                }
            }
            // 格式化日期
            item.setFormatEsDate(item.getEstimateTime() == null ? "-" : DateUtil.format(item.getEstimateTime(),"MM-dd"));
            item.setFormatNowDate(item.getValDate() == null ? "-" : DateUtil.format(item.getValDate(),"MM-dd"));
        });
        return Result.ok(pageList);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Result<FundList> detail(@RequestParam(name="code") String code){
        FundList data = fundListService.getOne(new LambdaQueryWrapper<FundList>().eq(FundList::getCode,code));
        return Result.OK(data);
    }

    @RequestMapping(value = "/getBuyGroup", method = RequestMethod.GET)
    public Result<List<Map<String,Object>>> getBuyGroup(){
        List<Map<String,Object>> data = fundListMapper.getBuyGroup();
        return Result.OK(data);
    }
}
